package com.powergisol.gis.controller;

import com.powergisol.gis.entity.TabCoordinateSystem;
import com.powergisol.gis.service.ExcelExService;
import com.powergisol.gis.service.ExcelInService;
import com.powergisol.gis.service.SearchService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.CollectionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.util.*;


@RestController
@RequestMapping(value = "excel")
@Api(description = "excel导入", value = "excel导入")
public class ExcelImportController extends BaseController {
    @Autowired
    private ExcelInService excelInService;
    @Autowired
    private SearchService searchService;
    @Autowired
    private ExcelExService excelExService;

    /**
     * 里正数据导入
     *
     * @param excelFile
     * @param vo
     * @param p
     * @return
     */
    @PostMapping("/lzImport")
    public Result lzImport(MultipartFile excelFile, SzZkCmdStandardVo vo, Principal p) {
        TabUserVo tabUserVo = getUser(p);
        return excelInService.easyexcelImport(excelFile, vo, tabUserVo);
    }

    /**
     * 城南院数据导入
     *
     * @param excelFile
     * @param vo
     * @param p
     * @return
     */
    @PostMapping("/cnyImport")
    public Result cnyImport(MultipartFile excelFile, CnyZkExcelStandardVo vo, Principal p) {
        TabUserVo tabUserVo = getUser(p);
        return excelInService.cnyImport(excelFile, vo, tabUserVo);
    }

    /**
     * cdm新标准
     *
     * @param excelFile
     * @param vo
     * @param p
     * @return
     */
    @PostMapping("/cnyImport1")
    public Result cnyImport1(MultipartFile excelFile, CnyZkExcelStandardVo vo, Principal p) {
        TabUserVo tabUserVo = getUser(p);
        return excelInService.cnyImport1(excelFile, vo, tabUserVo);
    }
    /**
     * 支柱梁数据导入
     *
     * @param excelFile
     * @param vo
     * @param p
     * @return
     */
    @PostMapping("/liangImport")
    public Result liangImport(MultipartFile excelFile, ExcelInportBaseVo vo, Principal p) {
        TabUserVo tabUserVo = getUser(p);
        return excelInService.liangImport(excelFile,vo,tabUserVo);
    }

    /**
     * 柱数据导入
     *
     * @param excelFile
     * @param vo
     * @param p
     * @return
     */
    @PostMapping("/zhuImport")
    public Result zhuImport(MultipartFile excelFile, ExcelInportBaseVo vo, Principal p) {
        TabUserVo tabUserVo = getUser(p);
        return excelInService.zhuImport(excelFile,vo,tabUserVo);
    }

    /**
     * excel数据导入
     *
     * @param excelFile
     * @param excelVo
     * @param p
     * @return
     */
    @PostMapping("/liveImport")
    public Result liveImport(MultipartFile excelFile, TabExcelVo excelVo, Principal p) {
        //TabUserVo tabUserVo = getUser(p);
        TabUserVo tabUserVo = new TabUserVo();
        tabUserVo.setName("admin");
        tabUserVo.setId(1);
        return excelInService.liveImport(excelFile,excelVo,tabUserVo);
    }

    @RequestMapping(value = "getCoorForm", method = RequestMethod.GET)
    @ApiOperation(value = "获取坐标转换公式", notes = "获取坐标转换公式")
    public Result getCoorForm() {
        return excelInService.getCoorForm();
    }

    @RequestMapping(value = "saveCoorForm", method = RequestMethod.GET)
    @ApiOperation(value = "获取坐标转换公式", notes = "获取坐标转换公式")
    public Result saveCoorForm(TabCoordinateSystem system) {
        return excelInService.saveCoorForm(system);
    }

    @RequestMapping(value = "getMould", method = RequestMethod.GET)
    @ApiOperation(value = "获取excel模板", notes = "获取excel模板")
    public Result getMould() {
        return excelInService.listMould();
    }

    @RequestMapping(value = "listByMouldId", method = RequestMethod.POST)
    @ApiOperation(value = "根据模板id获取模板详情设置", notes = "根据模板id获取模板详情设置")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "模板id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result listByMouldId(Integer id) {
        return excelInService.listByMouldId(id);
    }

    @RequestMapping(value = "saveMould", method = RequestMethod.POST)
    @ApiOperation(value = "保存模板设置", notes = "保存模板设置")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "mouldName", value = "模板名称", defaultValue = "0", required = true, paramType = "query")
    })
    public Result saveMould(@RequestBody TabMouldDetailVo vo) {
        return excelInService.saveMould(vo);
    }

    @RequestMapping(value = "upMould", method = RequestMethod.POST)
    @ApiOperation(value = "修改模板设置", notes = "修改模板设置")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "模板id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result upMould(@RequestBody TabMouldDetailVo vo) {
        return excelInService.upMould(vo);
    }

    @RequestMapping(value = "delMould", method = RequestMethod.POST)
    @ApiOperation(value = "删除模板设置", notes = "删除模板设置")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "模板id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result delMould(Integer id) {
        return excelInService.delMould(id);
    }


    @RequestMapping(value = "searchCol", method = RequestMethod.GET)
    @ApiOperation(value = "获取钻孔、土层、水位表的字段", notes = "获取钻孔、土层、水位表的字段")
    public Result searchCol() {
        Result result = new Result();
        List<Column> listZk = searchService.getColumns("tab_zuankong");
        List<Column> listTc = searchService.getColumns("tab_zk_tuceng");
        List<Column> listSw = searchService.getColumns("tab_zk_shuiwei");
        List<Column> listProject = searchService.getColumns("tab_project");
        Map<String, Object> map = new HashMap<>();
        map.put("zuankong", listZk);
        map.put("tuceng", listTc);
        map.put("shuiwei", listSw);
        map.put("project", listProject);
        result.setData(map);
        return result;
    }

    /**
     * @param serviceids
     * @param type
     */
    @GetMapping("/exportfile")
    public void excelExportFiles(@RequestParam("serviceids") List<Integer> serviceids, @RequestParam("type") String type, HttpServletResponse response) {

        if ("1".equals(type)) {
            excelExService.excelExportFiles(serviceids, response);
        } else {
            excelExService.excelExportFilesTwo(serviceids, response);
        }
    }

    @RequestMapping(
            value = "/randomexfiles/{type}",
            method = RequestMethod.POST,
            headers = {"content-type=application/*"}
    )
    public void randomExportFiles(String randomExportVOS,@PathVariable("type") String type, HttpServletResponse response) {
        ObjectMapper mapper = new ObjectMapper();
        CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, RandomExportVO.class);
        List<RandomExportVO> randomExportList = null;
        try {
            randomExportList = mapper.readValue(randomExportVOS, listType);
        } catch (IOException ioException) {
            ioException.printStackTrace();
            return;
        }
        if ("1".equals(type)) {
            excelExService.randomExportFiles(randomExportList, response);
        } else {
            excelExService.randomExportFilesTWO(randomExportList, response);
        }
    }
}

